<template>
  <div class="edit">
    <el-row>
      <el-col :span="4">
        <div style="padding-top: 50px; text-align: center;">
          <el-link @click="$router.push({ name: 'paper' })" icon="el-icon-back" :underline="false" type="primary">返回</el-link>
        </div>
      </el-col>
      <el-col :span="16">
        <div style="padding-top: 50px;">
          <el-form :model="paper" label-width="100px">
            <el-form-item label="ID">
              <el-input disabled :value="paper.id" readonly></el-input>
            </el-form-item>
            <el-form-item label="标题">
              <el-input v-model="paper.title"></el-input>
            </el-form-item>
            <el-form-item label="简介">
              <el-input v-model="paper.summary" type="textarea" size="medium" :rows="5">
              </el-input>
            </el-form-item>
            <!-- <el-form-item label="文件名">
              <el-input 
                v-model="paper.fileName"
              >
                <template slot="append">.pdf</template>
              </el-input>
            </el-form-item> -->
            <el-form-item label="发表日期">
              <el-date-picker
                v-model="paper.publishedTime"
                type="month"
                placeholder="选择日期"
                :editable="false"
                :picker-options="pickerOptions"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="关键字">
              <el-tag
                :key="index"
                v-for="(keyword, index) in paper.keywords"
                closable
                :disable-transitions="false"
                @close="handleCloseByKeyword(keyword)">
                {{keyword}}
              </el-tag>
              <el-input
                class="input-new-tag"
                v-if="inputVisibleByKeyword"
                v-model="inputKeyword"
                ref="saveTagInputByKeyword"
                size="small"
                @keyup.enter.native="handleInputConfirmByKeyword"
                @blur="handleInputConfirmByKeyword"
              >
              </el-input>
              <el-button v-else class="button-new-tag" size="small" @click="showInputByKeyword">添加关键字</el-button>
            </el-form-item>

            <el-form-item label="作者">
              <el-tag
                :key="index"
                v-for="(author, index) in paper.authers"
                closable
                :disable-transitions="false"
                @close="handleCloseByAuthor(author)">
                {{author}}
              </el-tag>
              <el-input
                class="input-new-tag"
                v-if="inputVisibleByAuthor"
                v-model="inputAuthor"
                ref="saveTagInputByAuthor"
                size="small"
                @keyup.enter.native="handleInputConfirmByAuthor"
                @blur="handleInputConfirmByAuthor"
              >
              </el-input>
              <el-button v-else class="button-new-tag" size="small" @click="showInputByAuthor">添加作者</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleEdit">立即修改</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col :span="4">
       <div style="height: 1px;"></div>
      </el-col>
    </el-row>
    <copyright class="bottom-layer" />
  </div>
</template>

<script>
import { update } from '../api/v1/paperManagement'
export default {
  name: 'PaperEdit',
  data() {
    return {
      paper: {
        id: "",
        title: "",
        authers: [],
        publishedTime: "",
        keywords: [],
        summary: "",
        status: 0,
        downloads: 0,
        fileName: '',
        fileStoredName: ''
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        }
      },
      inputVisibleByKeyword: false,
      inputKeyword: '',
      inputVisibleByAuthor: false,
      inputAuthor: ''
    }
  },
  methods: {
    handleEdit() {
      const paper = {}
      Object.assign(paper, this.paper)
      let { fileName } = paper
      if(fileName && fileName.split('.').pop().toLocaleLowerCase() !== 'pdf') {
        fileName += '.pdf' 
        paper.fileName = fileName      
      }
      update(paper).then(res => {
        this.$message.success('修改成功！')
        sessionStorage.setItem('paper', JSON.stringify(paper))
      }).catch(e => {
        this.$message.error('修改失败!')
      })
    },
    handleCloseByKeyword(keyword) {
      this.paper.keywords.splice(this.paper.keywords.indexOf(keyword), 1);
    },
    showInputByKeyword() {
      this.inputVisibleByKeyword = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInputByKeyword.$refs.input.focus();
      });
    },
    handleInputConfirmByKeyword() {
      let inputKeyword = this.inputKeyword;
      if (inputKeyword) {
        this.paper.keywords.push(inputKeyword);
      }
      this.inputVisibleByKeyword = false;
      this.inputKeyword = '';
    },
    handleCloseByAuthor(author) {
      this.paper.authers.splice(this.paper.authers.indexOf(author), 1);
    },
    showInputByAuthor() {
      this.inputVisibleByAuthor = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInputByAuthor.$refs.input.focus();
      });
    },
    handleInputConfirmByAuthor() {
      let inputAuthor = this.inputAuthor;
      if (inputAuthor) {
        this.paper.authers.push(inputAuthor);
      }
      this.inputVisibleByAuthor = false;
      this.inputAuthor = '';
    }
  },
  created() {
    const paper = JSON.parse(sessionStorage.getItem('paper'))
    if(paper && Object.keys(paper).length) {
      if(paper.title) {
        paper.title = paper.title.replace(/<\/?.+?\/?>/g, '')
      }
      if(paper.summary) {
        paper.summary = paper.summary.replace(/<\/?.+?\/?>/g, '')
      }
      this.paper = paper
    }
  }
}
</script>

<style scoped>
.edit {
  position: relative;
  min-height: calc(100vh - 120px);
}
.input {
  margin-top: 20px;
}

.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>